class EventEmitter {
    constructor() {
        // 注册
        this.cache = {};
    }
    // 订阅事件
    on(name, cb) {
        // 订阅队列
        if (!this.cache[name]) {
            this.cache[name] = [];
        }
        this.cache[name].push(cb);
    }
    // 触发事件
    emit(name, ...args) {
        if (this.cache[name]) {
            // 调用队列函数
            this.cache[name].forEach((callback) => {
                callback(...args);
            });
        }
    }
    // 取消订阅
    off(name, cb) {
        if (!cb) {
            this.cache[name] = []
        } else {
            const callbacks = this.cache[name];
            const index = callbacks.indexOf(cb);
            if (index !== -1) {
                callbacks.splice(index, 1);
            }
        }
    }
}
let eventEmitter = new EventEmitter();
export { eventEmitter }